﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NationalInstruments.DAQmx;
using System.Timers;
using System.Collections;
using iCollect.Constants;

namespace iCollect.util
{
    class NIUtil
    {
        private Task myTask;    //A new Task is created when the start button is pressed
        private AnalogMultiChannelReader myAnalogReader;
        private Timer loopTimer = new Timer();
        private double[] data;
        private String[] channels;

        private static NIUtil niUtil;
        public static NIUtil getInstance()
        {
            if (niUtil == null)
            {
                niUtil = new NIUtil();
            }
            return niUtil;
        }

        public void initTask(String[] array)
        {
            try
            {
                //Create a new Task
                myTask = new Task();
                channels = array;
                foreach (String channel in array)
                {
                    String channelID = GlobalConstants.channelMap[channel].ToString();
                    //create virtual channel
                    myTask.AIChannels.CreateVoltageChannel(channelID, "",
                        (AITerminalConfiguration)(-1), Convert.ToDouble(-10),
                        Convert.ToDouble(10), AIVoltageUnits.Volts);
                }

                //Verify the Task
                myTask.Control(TaskAction.Verify);

                //Prepare the table for Data
                //InitializeDataTable(myTask.AIChannels, ref dataTable);
                //acquisitionDataGrid.DataSource = dataTable;

                myAnalogReader = new AnalogMultiChannelReader(myTask.Stream);
            }
            catch (DaqException exception)
            {
                myTask.Dispose();
            }
        }

        public void endTask()
        {
            myTask.Dispose();
        }

        public void readData()
        {
            try
            {
                //Read the available data from the channels
                data = myAnalogReader.ReadSingleSample();

                //Plot your data here
                dataToDataTable(data);
            }
            catch (DaqException exception)
            {
                myTask.Dispose();
            }
        }

        private void dataToDataTable(double[] sourceArray)
        {
            try
            {
                int channelCount = sourceArray.GetLength(0);
                object channelValue;
                for (int currentChannelIndex = 0; currentChannelIndex < channelCount; currentChannelIndex++)
                {
                    channelValue = sourceArray.GetValue(currentChannelIndex);
                    List<ArrayList> itemList = GlobalConstants.currentData[channels[currentChannelIndex]];
                    ArrayList timeValue = new ArrayList();
                    timeValue.Add(DateTime.Now);
                    timeValue.Add(channelValue);
                    itemList.Add(timeValue);

                    if (itemList.Count > GlobalConstants.displayCount)
                    {
                        itemList.RemoveAt(0);
                    }
                }
            }
            catch (Exception e)
            {
                myTask.Dispose();
            }
        }
    }
}
